package routers

import (
	"errors"
	"net/http"
	"strings"

	"shqsoft.cn/pasnj/base"
	"shqsoft.cn/pasnj/bi/api"
	"shqsoft.cn/pasnj/entity"

	"github.com/labstack/echo"
	mw "github.com/labstack/echo/middleware"
)

// SetupMiddlewares 设置中间件
func SetupMiddlewares(e *echo.Echo) {
	// e.Logger = base.Logger{logrus.StandardLogger()}
	// e.Use(base.Hook())
	// e.Use(mw.Recover())
	// e.Use(CanVisist())
	// e.Use(mw.CORSWithConfig(mw.CORSConfig{
	// 	AllowOrigins: []string{"*"},
	// 	AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE},
	// }))
	e.Use(mw.Logger())
	e.Use(mw.Recover())
	e.Use(CanVisist())
	e.Use(mw.CORSWithConfig(mw.CORSConfig{
		AllowOrigins: []string{"*"},
		AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE},
	}))

}

// CanVisist 是否可访问中间件
func CanVisist() echo.MiddlewareFunc {
	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) (err error) {
			req := c.Request()
			p := req.URL.Path
			if strings.Index(p, "/api") != 0 {
				return next(c)
			}
			var pasInfo entity.PASInfo
			err = base.DB.First(&pasInfo).Error
			if err != nil {
				return echo.NewHTTPError(http.StatusBadRequest, err.Error())
			}
			if pasInfo.IsProcessing {
				err = errors.New("系统正在处理数据，请稍后访问！")
				return c.HTML(299, err.Error())
			}
			return next(c)
		}
	}
}

// SetupRoutes 设置路由
func SetupRoutes(e *echo.Echo) {
	// Routes
	e.Static("/publicstatic", "public/static")
	e.Static("/", "public")
	e.File("/login", "public/index.html")
	e.File("/error", "public/index.html")
	e.File("/", "public/index.html")
	r := e.Group("api/restricted")
	r2 := e.Group("api")
	SetupMiddlewares(e)
	config := mw.JWTConfig{
		Claims:     &UserLoginClaims{},
		SigningKey: []byte("secret"),
	}
	r.Use(mw.JWTWithConfig(config))
	r.GET("", Restricted)
	r.POST("", Restricted)
	r.POST("/Find/:DaoName/:PageNumber/:PageCount", FindRouter)
	r.POST("/FindAll/:DaoName", FindAllRouter)
	r.POST("/Add/:DaoName", AddRouter)
	r.POST("/Update/:DaoName", UpdateRouter)
	r2.POST("/Upload/Delete/:FileName", UploadDeleteRouter)
	r.POST("/Delete/:DaoName", DeleteRouter)
	r2.POST("/ExportXlsx/:DaoName", ExportXlsxRouter)
	r.POST("/Deletes/:DaoName", DeletesRouter)
	r2.POST("/Upload/:Name", UploadRouter)
	r2.POST("/DataMap", DataMapRouter)
	r2.POST("/DataImport", DataImportRouter)
	r2.POST("/DataImportProcess", DataImportProcessRouter)
	r.POST("/SaveDatas/:DaoName", SaveDatasRouter)
	r.POST("/AccountDiviflow/Comp", AccountCompDiviRouter)
	r.POST("/AccountDiviflow/DelCompDivi", DelCompDiviRouter)
	r.POST("/AccountDiviflow/DelIndiDivi", DelIndiDiviRouter)
	r.POST("/AccountDiviflow/DeleteINDILOAN", DeleteINDILOANRouter)

	r.POST("/AccountDiviflow/DelCompDiviDk", DelCompDiviDkRouter)

	r.POST("/AccountDiviflow/Compkh", AccountCompkhDiviRouter)
	r.POST("/AccountDiviflow/SetIsmrOK", AccountGetSetIsmrOKDiviRouter)
	r.POST("/AccountDiviflow/SetIsmrNO", AccountGetSetIsmrNODiviRouter)
	r.POST("/AccountDiviflow/SetIndiIsmrOK", AccountGetSetIndiIsmrOKDiviRouter)
	r.POST("/AccountDiviflow/SetIndiIsmrNO", AccountGetSetIndiIsmrNODiviRouter)
	r.POST("/AccountDiviflow/Indi", AccountIndiDiviRouter)
	r.POST("/AccountDiviflow/Indikh", AccountIndikhDiviRouter)

	r.POST("/AccountDiviflow/GetFTP", GetFTPRouter)
	r.POST("/AccountDiviflow/GetMnlr", GetMnlrRouter)
	//r.POST("/ClientDiviWorkFolw/Indi/:DaoName", SaveIndiWorkFlowRouter)
	r2.POST("/login", Login)
	r2.POST("/reStart", ReStart)
	r.GET("/Menu/:DaoName/:ParentMenuID", MenuByParentRouter)
	r.GET("/Menu/All", MenuAllRouter)
	r2.GET("/SSRSReport/:PermID", SSRSReportRouter)
	r.GET("/NtlmOptions", NtlmOptionsRouter)
	r.GET("/ReportControls/:PermID", ReportControlsRouter)
	r.GET("/ReportControlsUrl", GetReportRouterUrl)
	r.POST("/Menu/TreeNode", GetMenuTree)
	r.POST("/Menu/TreeNode/User", GetMenuUserTree)
	r.GET("/Menu/CheckedRole/perms/:RoleID", GetRolePermsKeys)
	r.GET("/Menu/CheckedRole/users/:RoleID", GetRoleUsersKeys)
	r.POST("/Menu/RolePerms/:RoleID", SaveRolePerms)
	r.POST("/Menu/UserPerms/:UserID", SaveUserPerms)
	r.POST("/Menu/Password", ChangePassword)
	r.POST("/Menu/InitPassword", InitializePassword)
	r.POST("/Menu/StopUsing", StopUsing)
	r.POST("/Menu/SetUsingGhr", SetUsingGhr)
	r.GET("/UserPermissionTypes/:PermID", UserPermissionTypesRouter)
	r.POST("/AccountDivi/Comp", AccountCompDiviQtzbRouter)
	r.POST("/AccountDivi/Indi", AccountIndiDiviQtzbRouter)
	r.GET("/Index/All", IndexAllRouter)
	r.POST("/ClientDiviIndi/Indi", ClientIndiDiviRouter)
	r.POST("/ClientDiviComp/Comp", ClientCompDiviRouter)
	r.POST("/ClientDiviMin/Min", ClientMinDiviRouter)
	r.POST("/ClientDiviMin/MinXghm", ClientMinXghmDiviRouter)
	r.POST("/ClientDiviMin/MinXghmLs", ClientMinXghmLsDiviRouter)
	r.POST("/ClientDiviFana/IndiFana", ClientFanaDiviRouter)
	r.POST("/ClientDiviLoan/IndiLoan", ClientLoanDiviRouter)
	r.POST("/ClientCompDiviWorkFlow/Comp", ClientCompWorkFlowRouter)
	r.POST("/ClientCompDiviSubmitWorkFlow/Comp", ClientCompSubmitWorkFlowRouter)
	r.POST("/ClientCompRollWorkFlow/Comp", ClientCompRollWorkFlowRouter)
	r.POST("/CompWorkFlowStep/Comp", CompWorkFlowStep)
	r.POST("/ClientDiviWorkFlow/Indi", ClientIndiWorkFlowRouter)
	r.POST("/ClientDiviSubmitWorkFlow/Indi", ClientIndiSubmitWorkFlowRouter)
	r.POST("/ClientIndiRollWorkFlow/Indi", ClientIndiRollWorkFlowRouter)
	r.POST("/IndiWorkFlowStep/Indi", IndiWorkFlowStep)
	r.POST("/TIndiWorkFlowStep/Indi", TIndiWorkFlowStep)
	r.POST("/TCompWorkFlowStep/Comp", TCompWorkFlowStep)
	r.POST("/UserDiviChange/Divi", UserDiviChange)
	r.GET("/Menu/CheckedUser/perms/:UserID", GetUserPermsKeys)
	r.GET("/Menu/CheckedUser/roles/:UserID", GetUserRolesKeys)
	r.POST("/Menu/UserPerms/:UserID", SaveUserPerms)
	r.POST("/UpdateSet/FapuZsfl", UpdateFapuZsfl)
	r.POST("/UpdateSet/TYYWFtp", UpdateTyywFtp)
	r.GET("/WorkFlowComp/Comp", WorkFlowCompRouter)
	r.GET("/WorkFlowIndi/Indi", WorkFlowIndiRouter)
	r.GET("/DiviWorkFlowGetLogin", GetLogin)
	r.POST("/AccountDivi/IndiYgjsTz", IndiYgjsTz)
	r.POST("/AccountDivi/dfdw", AccountDfdwDiviRouter)
	r.POST("/AccountDivi/Gjyw", AccountGjywDiviQtzbRouter)
	r.POST("/UpDiviProcess/Divi", DataProcessRouter)
	r.POST("/CreateWorkFlow/UserNewOrgan", UserCreateWorkFlowRouter)
	r.POST("/UserOrganWorkFlowStep/UserOrgan", UserStepWorkFlowRouter)
	r.POST("/UserOrganSubmitWorkFlow/UserOrgan", UserSubmitWorkFlowRouter)
	r.POST("/UserOrganDiviSel/UserOrgan", UserImportDiviSelRouter)
	r.POST("/UserOrganUpdate/UserOrgan", UserOrganUpdateRouter)
	r.POST("/AlterSaveCompCile/Qylx", AccountCompClieQylxRouter)
	r.POST("/UpdateCompAccoInrt/Inrt", UpdateCompAccoInrtRouter)
	r.POST("/SetCompAccoIsys/Isys", SetCompAccoIsysRouter)
	r.POST("/IndiAccoDiviSq/IndiAccoDiviSq", IndiAccoDiviSqRouter)
	r.POST("/IndiAccoDiviSqBTY/IndiAccoDiviSqBTY", IndiAccoDiviSqBTYRouter)
	r.POST("/IndiAccoDiviSqTY/IndiAccoDiviSqTY", IndiAccoDiviSqTYRouter)
	r.POST("/CompClieKhgl/InsC", CompClieKhglInsCRouter)
	r.POST("/CompClieKhgl/InsGl", CompClieKhglInsGlRouter)
	r.POST("/CompClieKhglZx/InsC", CompClieKhglZxInsCRouter)
	r.POST("/CompClieKhglZx/InsGl", CompClieKhgZxlInsGlRouter)
	r.POST("/CompClieZhsymb/Ins", CompClieZhsymbInsRouter)
	r.POST("/CompClieZhsyMbsd/Ins", CompClieZhsyMbsdInsRouter)
	r.POST("/DelCompMainClie/DelCompMainClie", DelCompMainClieRouter)
	r.POST("/DelCompOtherClie/DelCompOtherClie", DelCompOtherClieRouter)
	r.POST("/delCompClieMain/delCompClieMain", delCompClieMainRouter)
	r.POST("/DelCompClieGl/DelCompClieGl", DelCompClieGlRouter)
	r.POST("/DelIndiClieGl/DelIndiClieGl", DelIndiClieGlRouter)
	r.POST("/delSymb/delSymb", delSymbRouter)
	r.POST("/defaultDate/defaultDate", defaultDateRouter)
	r.POST("/InsertList/InsertList", InsertListRouter)
	r.POST("/InsertListDate/InsertListDate", InsertListDateRouter)
	r.POST("/delSymbzx/delSymbzx", delSymbzxRouter)
	r.POST("/Scdgzhsysj/Scdgzhsysj", ScdgzhsysjRouter)
	r.POST("/Sczxzhsysj/Sczxzhsysj", SczxzhsysjRouter)
	r.POST("/AddUserEmail/AddUserEmail", AddUserEmailRouter)
	r.POST("/DelUserEmail/DelUserEmail", DelUserEmailRouter)
	r.POST("/sendMail/sendMail", sendMailRouter)
	r.POST("/Menu/GetSysUserName", GetSysUserNameRouter)
	r.POST("/CreateData/Divi", CreateDataRouter)
	r.POST("/ReImport/Divi", ReImportRouter)

	r.POST("/AccountDiviflow/Xghm", XghmRouter)
	r.POST("/AccountDiviflow/Xgyg", XgygRouter)

	r.POST("/UseLog", SetUseLogRouter)

	r.POST("/Execproc", ExecprocRouter)
	r.POST("/GetPermissionName", GetPermissionNameRouter)
	r.POST("/GetCompJc/GetCompJc", GetCompJcRouter)

	r.POST("/Execindi", ExecindiRouter)

	r2.POST("/eva/export", ExportEvaRouter)
	r2.POST("/eva/export", ExportEvaRouter)

	r.POST("/AddZxqyUserEmail/AddZxqyUserEmail", AddZxqyUserEmailRouter)
	r.POST("/DelZxqyUserEmail/DelZxqyUserEmail", DelZxqyUserEmailRouter)
	r.POST("/sendZxqyMail/sendZxqyMail", sendZxqyMailRouter)

	r.POST("/CompEditInrt/Add", CompEditInrtAddRouter)
	r.POST("/CompEditInrt/Edit", CompEditInrtEditRouter)
	r.POST("/AccountDiviflow/GetFTP", GetFTPRouter)
	r.POST("/AccountDiviflow/GetMnlr", GetMnlrRouter)

	r.POST("/AccountDiviflow/GetCPQZ", GetCPQZRouter)
	r.POST("/AccountDiviflow/GetKHPZ", GetKHPZRouter)
	r.POST("/AccountDiviflow/GetZYDB", GetZYDBRouter)
	r.POST("/AccountDiviflow/GetBZDB", GetBZDBRouter)
	r.POST("/AccountDiviflow/GetQX", GetQXRouter)
	r.POST("/AccountDiviflow/GetGM", GetGMRouter)
	r.POST("/AccountDiviflow/GetHY", GetHYRouter)
	r.POST("/AccountDiviflow/GetEVA", GetEVARouter)

	r.POST("/workflow/Create/:wfid", FolwCreate)
	r.POST("/workflow/Submit/:runID", FolwSubmit)
	r.POST("/workflow/BatchSubmit", FolwSubmitBatch)
	r.POST("/workflow/Abort", FolwAbort)
	r.POST("/workflow/Delete/:runID", FolwDelete)
	r.POST("/workflow/SendBack/:runID", FolwSendBack)
	r.GET("/workflow/Executor/:runID", GetFolwExecutor)
	r.POST("/workflow/Executor/:runID", SetFolwExecutor)
	//查看2021年7月之后的数据
	r.GET("/workflow/RunInfo/:runID", GetFolwRunInfo)
	//查看2021年7月之前的数据
	r.GET("/workflow/RunInfoBak/:runID", GetFolwRunInfoBak)

	r.POST("/HomePage/All", HomeRequestAllRouter)
	r.POST("/HomePage/fetchCardData", HomeRequestCardData)
	r.POST("/HomePage/fetchChartData", HomeRequestChartData)
	r.POST("/HomePage/2", HomeRequest2Router)
	r.POST("/BI/Query", api.Query)
	r2.POST("/BI/Export", api.Export)
	r.POST("/ReportParamSearch", ReportParamSearchRouter)
	r.POST("/SaveGh/SaveGh", SaveGhRouter)

	r.POST("/IndiClieDiviFlowNewApp", IndiClieDiviFlowNewAppRouter)
	r.POST("/IndiClieDiviFlowUpdateApp", IndiClieDiviFlowUpdateAppRouter)

	r.POST("/CompClieDiviFlowUpdate", CompClieDiviFlowUpdateRouter)
	r.POST("/JjTzsd", JjTzsdRouter)
	r.POST("/JjTzjs", JjTzjsRouter)
	r.POST("/Jjyjtz", JjyjtzRouter)
	r.POST("/JjTzsd2", JjTzsd2Router)
	r.POST("/JjTzjs2", JjTzjs2Router)
	r.POST("/Jjyjtz2", Jjyjtz2Router)
	r.POST("/Bxtztz", BxtztzRouter)

	r.POST("/workflow/IsManager/:permID", IsManagerRouter)

	r.POST("/workflow/GetCompIsDivi", GetCompIsDiviRouter)
	r.POST("/workflow/GetIndiIsDivi", GetIndiIsDiviRouter)

	r.POST("/report/info/:ConfigName", ReportInfoRouter)
	r.POST("/report/listData", ReportDataRouter)
	r.POST("/report/export", ReportExportRouter)
	r.POST("/report/divi", ReportDIVIRouter)
	r.POST("/report/acbl", ReportACBLRouter)

	r.POST("/lryc/maxdryf", GetLrycMaxdryf)
	r.POST("/lryc/setMnlrYc", SetMnlrYc)
	r.POST("/workflow/fetgetCompAccoDivi/:runID", getCompAccoDiviRouter)
	r.POST("/workflow/fetgetIndiAccoDivi/:runID", getIndiAccoDiviRouter)

	r2.GET("/attachment/:name/:key", GetAttachmentRouter)
	r2.DELETE("/attachment/:name/:key", DeleteAttachmentRouter)
	r2.POST("/attachment/:name/:key", UploadAttachmentRouter)
}
